﻿using System;
using InfoService.Enums;
using InfoService.Settings.Data;
using MediaPortal.GUI.Library;
using InfoService.Settings;

namespace InfoService.Utils
{
    public static class RecentlyAddedUtils
    {
        private static readonly Logger logger = Logger.GetInstance();

        public static void ReloadRecentlyAddedProperties()
        {
            logger.WriteLog("Reloading recently added properties", LogLevel.Debug, InfoServiceModul.RecentlyAdded);
            
            RecentlyAddedUtils.SaveLastRecentlyAddedItems();
            RecentlyAddedUtils.LoadLastRecentlyAddedItems();
        }

        public static void SaveLastRecentlyAddedItems()
        {
            logger.WriteLog("Saving recently added items", LogLevel.Debug, InfoServiceModul.RecentlyAdded);
           
            SettingsManager.Properties.RecentlyAddedSettings.RecentlyAdded.Clear();
            for (int i = 1; i <= 3; i++)
            {
                SettingsRecentlyAddedItem srai = new SettingsRecentlyAddedItem();
                srai.Title = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Title, "series", i));
                srai.Season = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Season, "series", i));
                srai.EpisodeNumber = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Episodenumber, "series", i));
                srai.EpisodeTitle = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Episodetitle, "series", i));
                if (!string.IsNullOrEmpty(srai.Title) && !string.IsNullOrEmpty(srai.Season) && !string.IsNullOrEmpty(srai.EpisodeNumber) && !string.IsNullOrEmpty(srai.EpisodeTitle))
                {
                    srai.Title = srai.Title.Trim();
                    srai.Season = srai.Season.Trim();
                    srai.EpisodeNumber = srai.EpisodeNumber.Trim();
                    srai.EpisodeTitle = srai.EpisodeTitle.Trim();
                    if (!string.IsNullOrEmpty(srai.Title) && !string.IsNullOrEmpty(srai.Season) && !string.IsNullOrEmpty(srai.EpisodeNumber) && !string.IsNullOrEmpty(srai.EpisodeTitle))
                    {
                        srai.Thumb = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Thumb, "series", i));
                        srai.Fanart = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Fanart, "series", i));
                        try
                        {
                            srai.DateAdded = DateTime.Parse(GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.DateAdded, "series", i)));
                        }
                        catch
                        {
                            srai.DateAdded = DateTime.Now;
                        }
                        srai.Type = RecentlyAddedType.Series;

                        logger.WriteLog(string.Format("Saving recently added item {0}:{1}/{2}/{3}/{4}:{5}:{6}:{7}", srai.Type, srai.Title, srai.Season, srai.EpisodeNumber, srai.EpisodeTitle, srai.Thumb, srai.Fanart, srai.DateAdded), LogLevel.Debug, InfoServiceModul.RecentlyAdded);

                        SettingsManager.Properties.RecentlyAddedSettings.RecentlyAdded.Add(srai);
                    }
                }
            }
            for (int i = 1; i <= 3; i++)
            {
                SettingsRecentlyAddedItem srai = new SettingsRecentlyAddedItem();
                srai.Title = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Title, "movie", i));
                if (!string.IsNullOrEmpty(srai.Title))
                {
                    srai.Title = srai.Title.Trim();
                    if (!string.IsNullOrEmpty(srai.Title)) 
                    {
                        srai.Thumb = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Thumb, "movie", i));
                        srai.Fanart = GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Fanart, "movie", i));
                        try
                        {
                            srai.DateAdded = DateTime.Parse(GUIPropertyManager.GetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.DateAdded, "movie", i)));
                        }
                        catch
                        {
                            srai.DateAdded = DateTime.Now;
                        }
                        srai.Type = RecentlyAddedType.Movie;

                        logger.WriteLog(string.Format("Saving recently added item {0}:{1}:{2}:{3}:{4}", srai.Type, srai.Title, srai.Thumb, srai.Fanart, srai.DateAdded), LogLevel.Debug, InfoServiceModul.RecentlyAdded);

                        SettingsManager.Properties.RecentlyAddedSettings.RecentlyAdded.Add(srai);
                    }
                }
            }
        }

        public static void LoadLastRecentlyAddedItems()
        {
            logger.WriteLog("Loading recently added items", LogLevel.Debug, InfoServiceModul.RecentlyAdded);

            int i = 1;
            foreach (SettingsRecentlyAddedItem item in SettingsManager.Properties.RecentlyAddedSettings.RecentlyAdded)
            {
                TimeSpan spanRecentlyAddedItem = DateTime.Now - item.DateAdded;
                if (spanRecentlyAddedItem.Days > 7)
                {
                    logger.WriteLog(string.Format("Skipping recently added item {0}:{1}, older than 7 days", item.Type, item.Title), LogLevel.Debug, InfoServiceModul.RecentlyAdded);
                    continue; // just skip item, it won't be saved when saving settings
                }
               
                switch (item.Type)
                {
                    case RecentlyAddedType.Series:
                        logger.WriteLog(string.Format("Loading recently added item {0}:{1}/{2}/{3}/{4}:{5}:{6}:{7}", item.Type, item.Title, item.Season, item.EpisodeNumber, item.EpisodeTitle, item.Thumb, item.Fanart, item.DateAdded), LogLevel.Debug, InfoServiceModul.RecentlyAdded);

                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Title, "series", i), item.Title);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Season, "series", i), item.Season);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Episodenumber, "series", i), item.EpisodeNumber);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Episodetitle, "series", i), item.EpisodeTitle);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Thumb, "series", i), item.Thumb);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Fanart, "series", i), item.Fanart);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.DateAdded, "series", i), item.DateAdded.ToShortDateString());
                        break;
                    case RecentlyAddedType.Movie:
                        logger.WriteLog(string.Format("Loading recently added item {0}:{1}:{2}:{3}:{4}", item.Type, item.Title, item.Thumb, item.Fanart, item.DateAdded), LogLevel.Debug, InfoServiceModul.RecentlyAdded);

                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Title, "movie", i), item.Title);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Thumb, "movie", i), item.Thumb);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.Fanart, "movie", i), item.Fanart);
                        PropertyUtils.SetProperty(string.Format(PropertyUtils.Properties.RecentlyAdded.DateAdded, "movie", i), item.DateAdded.ToShortDateString());
                        break;
                }
                i++;
            }
        }
    }
}
